package org.mathcloud.math.core.scilab.quantity;

import org.mathcloud.math.core.common.quantity.RealNumber;
import org.mathcloud.math.core.generic.signature.GenericQuantityObjectSignature;
import org.scilab.modules.javasci.JavasciException;
import org.scilab.modules.types.ScilabDouble;
import org.scilab.modules.types.ScilabType;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


public class ScilabRealNumber extends ScilabQuantityObject implements RealNumber 
{
	public ScilabRealNumber()
	{
		super();
		sciObject = new ScilabDouble(0.0);
		try
		{
			sciEngine.put(sciVarName, sciObject);
		} catch (JavasciException e)
		{
			log.error("Constructor - ",e);
		}
		signature = GenericQuantityObjectSignature.getRealNumberSignature();
		signature.setRealizationName("scilab");
	}
	
	public ScilabRealNumber(double in)
	{
		super();
		try
		{
			sciObject = new ScilabDouble(in);
			sciEngine.put(sciVarName, sciObject);
		} catch (JavasciException e)
		{
			log.error("Constructor - ",e);
		}
		signature = GenericQuantityObjectSignature.getRealNumberSignature();
		signature.setRealizationName("scilab");
	}
	
	public void setValue(double in)
	{
		try
		{
			sciObject = new ScilabDouble(in);
			log.info(getVarName()+"="+in+";");
			sciEngine.put(sciVarName, sciObject);
		} catch (JavasciException e)
		{
			log.error("Set value - ",e);
		}
	}

	@Override
	public Double getValue() 
	{
		if ((sciEngine!=null)&&(sciVarName!=null))
		{
			try
			{
				ScilabType res = sciEngine.get(sciVarName);
				if (res instanceof ScilabDouble)
				{
					sciObject = (ScilabDouble)res;
					if (!sciObject.isEmpty())
						return sciObject.getRealPart()[0][0];
					else return null;
				}
				else return null;
			} catch (JavasciException e)
			{
				log.error("Get value - ",e);
				return null;
			}
		}
		else return null;
	}
	
	private static final Logger log = LoggerFactory.getLogger(ScilabRealNumber.class);
}
